#include "zf_pint.h"

//-------------------------------------------------------------------------------------------------------------------
//	@brief		PINT 功能初始化
//	@param		pint_index		所选 PIT (INTERRUPT_PINT0 - INTERRUPT_PINT7)
//	@param		mode			中断模式 (PINT_RISE_EDGE - PINT_HIGH_LEVEL)
//	@param		handler			自拟定中断服务函数
//	Sample usage:				zf_pint_init(INTERRUPT_PINT0, PINT_RISE_EDGE, handler);
//-------------------------------------------------------------------------------------------------------------------
void zf_pint_init (interrupt_pint_list pint_index, pint_mode_list mode, void handler(void))
{
	static bool pint_flag = false;	
	assert(handler != NULL);
	pint_irqn_func[pint_index] = handler;																// 绑定中断服务函数

	if(pint_flag == 0)																					// 如果是初次调用
	{
		PINT_Init(PINT);																				// 初始化 PINT
		pint_flag = true;																				// PINT 已初始化
	}
	PINT_PinInterruptConfig(PINT, (pint_pin_int_t)pint_index, (pint_pin_enable_t)mode, NULL);			// 写入对应 PINT 中断方式
}

//-------------------------------------------------------------------------------------------------------------------
//	@brief		PINT 功能管脚选定
//	@param		pint_index		所选 PIT (INTERRUPT_PINT0 - INTERRUPT_PINT7)
//	@param		group_index		管脚组别
//	@param		pin_index		管脚索引
//	Sample usage:				zf_pint_pin_set(INTERRUPT_PINT0, PIO0, PIN00);
//-------------------------------------------------------------------------------------------------------------------
void zf_pint_pin_set (interrupt_pint_list pint_index, group_index_list group_index, pin_index_list pin_index)
{
	uint32_t temp = ((PINTSEL0 << PMUX_SHIFT) + pin_index);
	if(group_index == PIO1)	temp += 32U;
	zf_gpio_default_init(group_index, pin_index, PIN_DIR_IN);

	INPUTMUX_Init(INPUTMUX);																			// 使能 INPUTMUX
	INPUTMUX_AttachSignal(INPUTMUX, pint_index, temp);													// 将对应 IO 信号路由到对应 PINT
	INPUTMUX_Deinit(INPUTMUX);
}

//-------------------------------------------------------------------------------------------------------------------
//	@brief		PINT 功能使能
//	@param		pint_index		所选 PIT (INTERRUPT_PINT0 - INTERRUPT_PINT7)
//	Sample usage:				zf_pint_interrupt_enable(INTERRUPT_PINT0);
//-------------------------------------------------------------------------------------------------------------------
void zf_pint_interrupt_enable (interrupt_pint_list pint_index)
{
	PINT_EnableCallbackByIndex(PINT, (pint_pin_int_t)pint_index);
}

//-------------------------------------------------------------------------------------------------------------------
//	@brief		PINT 功能关闭
//	@param		pint_index		所选 PIT (INTERRUPT_PINT0 - INTERRUPT_PINT7)
//	Sample usage:				zf_pint_interrupt_disable(INTERRUPT_PINT0);
//-------------------------------------------------------------------------------------------------------------------
void zf_pint_interrupt_disable (interrupt_pint_list pint_index)
{
	PINT_DisableCallbackByIndex(PINT, (pint_pin_int_t)pint_index);
}
